package dpss.replicamanager.gui;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.net.InetSocketAddress;
import java.util.Vector;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import dpss.replicamanager.data.ServerNodes;

/**
 * The popup menu when a server node is clicked or rightclicked
 * 
 * @author musab.mirza
 * 
 */
public class ServerNodePopup extends JPopupMenu {

	private JMenuItem init_na, init_eur, init_asia, uninitialize, disconnect, makeFaulty, makeUnfaulty;
	public JMenuItem getMakeFaulty() {
		return makeFaulty;
	}


	public JMenuItem getMakeUnfaulty() {
		return makeUnfaulty;
	}

	private Vector clickedRowData;

	/**
	 * The constructer creates the menu based on the data provided
	 * 
	 * @param clickedRowData
	 */
	public ServerNodePopup() {
		
		this.clickedRowData = null;

		init_asia = new JMenuItem("Initialize ASIA");
		init_eur = new JMenuItem("Initialize EUR");
		init_na = new JMenuItem("Initialize NA");
		uninitialize = new JMenuItem("Uninitialize");
		makeFaulty = new JMenuItem("Make faulty");
		makeUnfaulty = new JMenuItem("Make unfaulty");
		disconnect = new JMenuItem("Disconnect");

		init_asia.setBackground(Color.green);
		init_eur.setBackground(Color.green);
		init_na.setBackground(Color.green);
		uninitialize.setBackground(Color.cyan);
		makeFaulty.setForeground(Color.red);
		makeUnfaulty.setForeground(Color.green);
		disconnect.setBackground(Color.red);

	}

	
	public JMenuItem getInit_na() {
		return init_na;
	}

	public JMenuItem getInit_eur() {
		return init_eur;
	}

	public JMenuItem getInit_asia() {
		return init_asia;
	}

	public JMenuItem getUninitialize() {
		return uninitialize;
	}

	public JMenuItem getDisconnect() {
		return disconnect;
	}

	/**
	 * Prepares the menu based on the data provided
	 * 
	 * @param clickedRowData
	 * @param e 
	 */
	public void doPopup(Vector clickedRowData, MouseEvent e) {
		
		this.clickedRowData = clickedRowData;

		String nodeGroup = ((String) (clickedRowData
				.get(Constants.LISTING_COLUMN_GROUP))).toLowerCase();
		boolean isLeader = (boolean) clickedRowData.get(Constants.LISTING_COLUMN_LEADER);

		if (isLeader){
			GUI.showError("You cannot modify the leader once it's initialized!");
			return;
		}
		
		remove(init_asia);
		remove(init_eur);
		remove(init_na);
		remove(uninitialize);
		remove(disconnect);
		remove(makeFaulty);
		remove(makeUnfaulty);
		
		if (nodeGroup.equalsIgnoreCase("uninitialized")) {
			add(init_asia);
			add(init_eur);
			add(init_na);
		} else {
			
			if ((boolean) clickedRowData.get(Constants.LISTING_COLUMN_FAULTY) && !(boolean) clickedRowData.get(Constants.LISTING_COLUMN_LEADER))
				add(makeUnfaulty);
			else
				add(makeFaulty);
			add(uninitialize);
		}
		add(disconnect);
		show(e.getComponent(), e.getX(), e.getY());
		
	}

	/**
	 * Returns the selection key in the form of InetSocketAddress
	 * @return
	 */
	public InetSocketAddress getSelectionKey() {
		return ServerNodes.string2InetSocketAddress((String) clickedRowData.get(Constants.LISTING_COLUMN_ADDRESS));
	}
	

}
